package nongchang.kuaijiecaifu.com.ncapp_android.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ezy.lite.util.ContextUtil;
import nongchang.kuaijiecaifu.com.ncapp_android.Const;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.activity.BaseActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.HomeCartManager;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.AddressEvent;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.AllCart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Cart;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter.UserPayExpandAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.popup.SureOrderDialog;
import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;

public class UserPayActivity extends BaseActivity {
    @BindView(R.id.expand_listview)
    ExpandableListView expandableListView;
    @BindView(R.id.txt_price)
    TextView txtPrice;
    private UserPayExpandAdapter adapter;
    private List<AllCart> list;
    private float totalmoney;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_pay);
        ButterKnife.bind(this);
        list=new ArrayList<>();

        for(int i=0,size=HomeCartManager.cartList.size();i<size;i++){
            if(HomeCartManager.cartList.get(i).isSelect){
                list.add(HomeCartManager.cartList.get(i));
            }
        }
        adapter=new UserPayExpandAdapter(list,this);
        RelativeLayout address= (RelativeLayout) getLayoutInflater().inflate(R.layout.item_cart_address, null, false);
        TextView textView= (TextView) address.findViewById(R.id.txt_all_address);
        TextView txtFirst = (TextView)address.findViewById(R.id.txt_first);
        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               ContextUtil.startActivity(v.getContext(),UserAddAddressActivity.class);
            }
        });
        LinearLayout cart_data= (LinearLayout) getLayoutInflater().inflate(R.layout.item_cart_message, null, false);
        TextView txtMoney = (TextView)cart_data.findViewById(R.id.txt_money);

        expandableListView.addHeaderView(address);
        expandableListView.addFooterView(cart_data);
        expandableListView.setAdapter(adapter);
        expandableListView.setGroupIndicator(null);


        int money=0;
        for(int i=0,size=list.size();i<size;i++){
            money+=list.get(i).money;
        }
        txtMoney.setText("￥"+money);
        totalmoney=money+6;
        txtPrice.setText("总价：￥"+totalmoney);
        RxBus.ofType(AddressEvent.class).compose(bindToLifecycle()).subscribe(event->{
            txtFirst.setVisibility(View.GONE);
            textView.setText("收货人："+event.name+event.phone+"\n收货地址："+event.address+"\n收货时间："+event.time);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(list!=null){
            list.clear();
            list=null;
        }
        if(adapter!=null){
            adapter=null;
        }
    }

    @OnClick({R.id.btn_submit,R.id.btn_back})
    void onClick(View view){
        switch (view.getId()){
            case R.id.btn_submit:
                    new SureOrderDialog(this).setData(totalmoney).show();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
